<?php
/**
 * [商网通 System] Copyright swtshop.cn
 * QQ: 1170787711
 * Author: gj.name
 * This is NOT a freeware
*/
defined('IN_UQ') or exit('Access Denied');
class goods_order_uqcms extends control
{
    function __construct()
    {
        $this->aid = $_SESSION['admin']['aid'];
        parent::__construct();
    }
    public function index()
    {
        $page = isset($_GET['page']) ? $_GET['page'] : '1';
        $page_size = isset($_GET['page_size']) ? $_GET['page_size'] : '15';
        $status = isset($_GET['status']) ? $_GET['status'] : '';
        $order_no = isset($_GET['order_no']) ? $_GET['order_no'] : '';
        $wsql = '';
        if ($status) {
            $wsql .= ' order_status = ' . $status . ' ';
        } elseif ($status == '0') {
            $wsql .= ' order_status = 0 ';
        }
        if ($order_no) {
            $wsql .= " order_no = '" . $order_no . "'";
        }
        if (!empty($wsql)) {
            $wsql = ' where ' . rtrim($wsql, ' and ');
        }
        $result = $this->db->get_all("select * from " . table("order_info") . $wsql . " order by order_no desc limit " . ($page - 1) * $page_size . "," . $page_size);
        if ($result) {
            foreach ($result as $k => $v) {
                $sp_arr[] = $v['shop_id'];
            }
            if ($sp_arr) {
                $sp_in = implode(',', $sp_arr);
            }
            $sp_result = $this->db->get_all("select id,name from " . table('shop') . " where id in (" . $sp_in . ")");
            foreach ($sp_result as $k => $v) {
                $sp_name_arr[$v['id']] = $v['name'];
            }
            foreach ($result as $k => $v) {
                $v['shop_name'] = $sp_name_arr[$v['shop_id']];
                $v['order_status_cn'] = $this->module('order')->order_status($v['order_status']);
                $v['addtime'] = _date($v['addtime']);
                $v['pay_time'] = _date($v['pay_time']);
                $list[] = $v;
            }
            $data['list'] = $list;
            $total = $this->db->get_one("select count(id) as total from " . table('order_info') . $wsql);
            if ($total > $page_size) {
                $data['page'] = $this->uqcms_page(array('page' => $page, 'page_size' => $page_size, 'total' => $total['total']));
            }
            $this->assign('data', $data);
        }
        $this->display();
    }
    function detail()
    {
        $id = isset($_GET['id']) ? $_GET['id'] : exit('订单不能为空');
        $row = $this->db->get_one("select * from " . table('order_info') . " where id = " . $id);
        $row['order_status_cn'] = $this->module("order")->order_status($row['order_status']);
        $order_list = $this->db->get_all("select * from " . table('order_goods') . " where oid = " . $row['id']);
        if ($order_list) {
            $barcode_in = '';
            foreach ($order_list as $v) {
                $barcode_in .= "'" . $v['barcode'] . "',";
            }
            $barcode_in = rtrim($barcode_in, ',');
            $stock_list = $this->db->get_all("select * from " . table('stock_product') . " where barcode in(" . $barcode_in . ")");
            if ($stock_list) {
                foreach ($stock_list as $v) {
                    $stock_arr[$v['barcode']]['num'] = $v['stock_num'];
                    $stock_arr[$v['barcode']]['title'] = $v['title'];
                }
            }
            foreach ($order_list as $v) {
                if ($v['sku_id'] != 0) {
                    $v['sku'] = unserialize($v['sku_value']);
                }
                if (@$stock_arr[$v['barcode']]['num'] >= $v['buy_num']) {
                    $v['stock_num_cn'] = $stock_arr[$v['barcode']]['num'];
                } else {
                    $v['stock_num_cn'] = '库存不足';
                }
                $v['stock_title'] = @$stock_arr[$v['barcode']]['title'];
                $data[] = $v;
            }
        }
        $row['goods'] = $data;
        $addr_row = $this->db->get_one("select * from " . table('order_address') . " where oid = " . $row['id']);
        $addr_row['area'] = $addr_row['prov_cn'] . ',' . $addr_row['city_cn'] . ',' . $addr_row['dist_cn'];
        $row['addr'] = $addr_row;
        if ($row['uid'] == '0') {
            $urow = $this->db->get_one("select uid,mobile from " . table('member') . " where uid = " . $row['uid']);
            $row['user'] = $urow;
        }
        $row['pay_time'] = _date($row['pay_time']);
        $row['deliver_time'] = _date($row['deliver_time']);
        $row['receipt_time'] = _date($row['receipt_time']);
        $row['comment_time'] = _date($row['comment_time']);
        $row['addtime'] = _date($row['addtime']);
        $this->assign('row', $row);
        $this->display();
    }
    function del()
    {
        $order_id = isset($_POST['id']) ? $_POST['id'] : error_json('提交编号不能为空');
        $query = $this->db->get_all("select id,order_status from " . table('order_info') . " where order_status = 0 and id in(" . $order_id . ")");
        if ($query) {
            $this->db->autocommit();
            foreach ($query as $v) {
                $q1 = $this->db->delete(table('order_info'), 'id = ' . $v['id']);
                $q2 = $this->db->delete(table('order_goods'), 'oid = ' . $v['id']);
                $q3 = $this->db->delete(table('order_address'), 'oid = ' . $v['id']);
                $q4 = $this->db->delete(table('order_score'), 'oid = ' . $v['id']);
                if ($q1 && $q2 && $q3 && $q4) {
                    $run[] = '';
                }
            }
            if (count($query) == count($run)) {
                $this->db->commit();
                right_json();
            } else {
                $this->db->rollback();
                error_json('删除失败');
            }
        } else {
            error_json('无执行条数');
        }
    }
    function check()
    {
        $status = isset($_GET['status']) ? $_GET['status'] : '1';
        if ($status == '1' || $status == '') {
            $cancel_time = cfg('sys_cancel_order_time');
            $overtime = time() - $cancel_time;
            $q = $this->db->get_one("select count(id) as total from " . table('order_info') . " where order_status = 1 and addtime > " . $overtime);
            $q2 = $this->db->get_one("select count(id) as total from " . table('order_info') . " where order_status = 1 and addtime < " . $overtime);
        } elseif ($status == '2') {
            $cancel_time = cfg('sys_receipt_time');
            $overtime = time() - $cancel_time;
            $q = $this->db->get_one("select count(id) as total from " . table('order_info') . " where order_status = 3 and deliver_time > " . $overtime);
            $q2 = $this->db->get_one("select count(id) as total from " . table('order_info') . " where order_status = 3 and deliver_time < " . $overtime);
        } elseif ($status == '3') {
            $cancel_time = cfg('sys_cancel_order_time');
            $overtime = time() - $cancel_time;
            $q = $this->db->get_one("select count(id) as total from " . table('promote_group_order') . " where status = 0 and start_time > " . $overtime);
            $q2 = $this->db->get_one("select count(id) as total from " . table('promote_group_order') . " where status = 0 and start_time < " . $overtime);
        }
        $row['total_yx'] = $q['total'];
        $row['total_gq'] = $q2['total'];
        $this->assign('row', $row);
        $this->display();
    }
    function check_run()
    {
        $status = isset($_GET['status']) ? $_GET['status'] : '1';
        $limit = isset($_GET['limit']) ? $_GET['limit'] : '15';
        $this->display();
        ob_flush();
        flush();
        if ($status == '1') {
            $cancel_time = cfg('sys_cancel_order_time');
            $overtime = time() - $cancel_time;
            $query = $this->db->get_all("select id,order_status,addtime from " . table('order_info') . " where order_status = 1 and addtime < " . $overtime . " limit " . $limit);
            if ($query) {
                foreach ($query as $v) {
                    $run_result = $this->module('order')->cancel_order_run($v['id'], '自动取消订单');
                    if ($run_result) {
                        $this->ajax_load($v['id']);
                    }
                }
                echo '<script type=\\"text/javascript\\">location.reload();</script>';
            } else {
                $this->ajax_end();
            }
        } elseif ($status == '2') {
            $cancel_time = cfg('sys_receipt_time');
            $overtime = time() - $cancel_time;
            $query = $this->db->get_all("select * from " . table('order_info') . " where order_status = 3 and deliver_time < " . $overtime . " limit " . $limit);
            if ($query) {
                foreach ($query as $v) {
                    $run_result = $this->module('order')->receipt_goods_run($v['id'], $v['uid'], $v['shop_id'], $v['total_price']);
                    if ($run_result) {
                        $this->ajax_load($v['id']);
                    }
                }
                echo '<script type=\\"text/javascript\\">location.reload();</script>';
            } else {
                $this->ajax_end();
            }
        } elseif ($status == '3') {
            $cancel_time = cfg('sys_cancel_order_time');
            $overtime = time() - $cancel_time;
            $query = $this->db->get_all("select id,status,start_time from " . table('promote_group_order') . " where status = 0 and start_time < " . $overtime . " limit " . $limit);
            if ($query) {
                foreach ($query as $v) {
                    $run_result = $this->module('order')->group_cancel_order($v['id']);
                    if ($run_result) {
                        $this->ajax_load($v['id']);
                    }
                }
                echo '<script type=\\"text/javascript\\">location.reload();</script>';
            } else {
                $this->ajax_end();
            }
        }
    }
    function ajax_load($code)
    {
        $msg = '内部编号：' . $v['id'] . ' .. 处理完成。';
        echo "<script type=\"text/javascript\">uprogress('" . $msg . "');</script>";
        ob_flush();
        flush();
    }
    function ajax_end()
    {
        $status = isset($_GET['status']) ? $_GET['status'] : '1';
        $url = '?m=goods_order&a=check&status=' . $status;
        echo "<script type=\"text/javascript\">uprogress('已经执行完成 &nbsp;&nbsp;&nbsp;<a href=\"?m=goods_order&a=check&status=" . $status . "\">返回</a>');</script>";
        ob_flush();
        flush();
    }
    public function statistic()
    {
        $time = time_range::today();
        $sql = "SELECT COUNT(DISTINCT id) order_count, FROM_UNIXTIME(addtime, '%Y-%m-%d %H:00:00') AS hours FROM " . table('order_info') . " WHERE addtime >= " . $time['stime'] . " and addtime <= " . $time['etime'] . " GROUP BY hours ORDER BY addtime";
        $day_all = $this->db->get_all($sql);
        if ($day_all) {
            foreach ($day_all as $k => $v) {
                $count_arr[strtotime($v['hours'])] = $v['order_count'];
            }
        }
        for ($i = 0; $i < 24; $i++) {
            $day_time = $time['stime'] + $i * 3600;
            $dt['time'] = date('H', $day_time);
            if (isset($count_arr[$day_time])) {
                $dt['count'] = $count_arr[$day_time];
            } else {
                $dt['count'] = '0';
            }
            $data[] = $dt;
        }
        foreach ($data as $k => $v) {
            $timearr[] = $v['time'];
            $countarr[] = $v['count'];
        }
        $result['time'] = implode(',', $timearr);
        $result['count'] = implode(',', $countarr);
        $this->assign('js_tj', $result);
        $this->display();
    }
    function refund()
    {
        $page = isset($_GET['page']) ? $_GET['page'] : '1';
        $page_size = isset($_GET['page_size']) ? $_GET['page_size'] : '20';
        $status = isset($_GET['status']) ? $_GET['status'] : '';
        $service_id = isset($_GET['service_id']) ? $_GET['service_id'] : '';
        $wsql = '';
        if ($status) {
            $wsql = ' status = ' . $status . ' ';
        } elseif ($status == '0') {
            $wsql = ' status = 0 ';
        }
        if ($service_id) {
            $wsql = " service_id = '" . $service_id . "'";
        }
        if (!empty($wsql)) {
            $wsql = ' where ' . rtrim($wsql, ' and ');
        }
        $result = $this->db->get_all("select * from " . table("refund") . $wsql . " order by id desc limit " . ($page - 1) * $page_size . "," . $page_size);
        if ($result) {
            foreach ($result as $k => $v) {
                $sp_arr[] = $v['shop_id'];
            }
            if ($sp_arr) {
                $sp_in = implode(',', $sp_arr);
            }
            $sp_result = $this->db->get_all("select id,name from " . table('shop') . " where id in (" . $sp_in . ")");
            foreach ($sp_result as $k => $v) {
                $sp_name_arr[$v['id']] = $v['name'];
            }
            foreach ($result as $k => $v) {
                $v['shop_name'] = $sp_name_arr[$v['shop_id']];
                $v['status_cn'] = $this->module('refund')->refund_status($v['status']);
                $v['addtime'] = _date($v['addtime']);
                $v['oktime'] = _date($v['oktime']);
                $list[] = $v;
            }
            $data['list'] = $list;
            $total = $this->db->get_one("select count(id) as total from " . table('refund') . $wsql);
            if ($total < $page_size) {
                $data['page'] = $this->uqcms_page(array('page' => $page, 'page_size' => $page_size, 'total' => $total['total']));
            }
            $this->assign('data', $data);
        }
        $this->display();
    }
    public function refund_show()
    {
        $id = isset($_GET['id']) ? $_GET['id'] : false;
        if ($id) {
            $row = $this->db->get_one("select * from " . table('refund') . " where id = " . $id);
            $row['addtime'] = _date($row['addtime']);
            $row['passtime'] = _date($row['passtime']);
            $row['sendtime'] = _date($row['sendtime']);
            $row['admintime'] = _date($row['admintime']);
            $row['oktime'] = _date($row['oktime']);
            $row['reason_cn'] = $this->module('refund')->refund_reason($row['reason']);
            $this->assign('row', $row);
            $process_log = $this->db->get_all("select * from " . table('refund_process') . " where rid = " . $id);
            $this->assign('process', $process_log);
        }
        $this->display();
    }
    public function refund_save()
    {
    }
    public function refund_del()
    {
        $this->db->delete(table('goods_comment'), 'id = ' . $id);
    }
    function comment()
    {
        $page = isset($_GET['page']) ? $_GET['page'] : '1';
        $page_size = isset($_GET['page_size']) ? $_GET['page_size'] : '15';
        $status = isset($_GET['status']) ? $_GET['status'] : '';
        $wsql = '';
        if ($status) {
            $wsql = ' status = ' . $status . ' ';
        } elseif ($status == '0') {
            $wsql = ' status = 0 ';
        }
        if (!empty($wsql)) {
            $wsql = ' where ' . rtrim($wsql, ' and ');
        }
        $result = $this->db->get_all("select * from " . table("goods_comment") . $wsql . " order by id desc limit " . ($page - 1) * $page_size . "," . $page_size);
        if ($result) {
            foreach ($result as $k => $v) {
                $sp_arr[] = $v['shop_id'];
            }
            if ($sp_arr) {
                $sp_in = implode(',', $sp_arr);
            }
            $sp_result = $this->db->get_all("select id,name from " . table('shop') . " where id in (" . $sp_in . ")");
            foreach ($sp_result as $k => $v) {
                $sp_name_arr[$v['id']] = $v['name'];
            }
            foreach ($result as $k => $v) {
                $v['shop_name'] = $sp_name_arr[$v['shop_id']];
                $v['addtime'] = _date($v['addtime']);
                $list[] = $v;
            }
            $data['list'] = $list;
            $total = $this->db->get_one("select count(id) as total from " . table('goods_comment') . $wsql);
            if ($total > $page_size) {
                $data['page'] = $this->uqcms_page(array('page' => $page, 'page_size' => $page_size, 'total' => $total['total']));
            }
            $this->assign('data', $data);
        }
        $this->display();
    }
    function comment_edit()
    {
        $id = isset($_GET['id']) ? $_GET['id'] : exit('COMMENT ID IS ERROR');
        $row = $this->db->get_one("select * from " . table('goods_comment') . " where id = " . $id);
        $this->assign('row', $row);
        $this->display();
    }
    function comment_save()
    {
        if ($_POST) {
            $data['content'] = !empty($_POST['content']) ? $_POST['content'] : exit('内容不能为空');
            $data['score'] = !empty($_POST['score']) ? $_POST['score'] : exit('评分不能为空');
            $data['reply'] = isset($_POST['reply']) ? $_POST['reply'] : '';
            $data['status'] = isset($_POST['status']) ? $_POST['status'] : '1';
            $id = isset($_POST['id']) ? $_POST['id'] : exit('提交错误');
            $query = $this->db->update(table('goods_comment'), $data, 'id = ' . $id);
            if ($query) {
                exit('1');
            } else {
                exit('保存失败');
            }
        } else {
            exit('保存失败');
        }
    }
    function comment_del()
    {
        $id = isset($_POST['id']) ? $_POST['id'] : exit('提交不能为空');
        $this->db->commit();
        $call = $this->db->get_all("select * from " . table('goods_comment_img') . " where comment_id in(" . $id . ")");
        if ($call) {
            foreach ($call as $v) {
                $this->db->update(table('album'), array('type' => '0', 'status' => '0'), "url = '" . $v['images'] . "'");
            }
        }
        $d1 = $this->db->delete(table('goods_comment'), 'id in(' . $id . ')');
        $d2 = $this->db->delete(table('goods_comment_img'), 'comment_id in(' . $id . ')');
        if ($d1 && $d2) {
            $this->db->autocommit();
            right_json();
        } else {
            $this->db->rollback();
            error_json('删除失败');
        }
    }
    function seller()
    {
        $page = isset($_GET['page']) ? $_GET['page'] : '1';
        $page_size = isset($_GET['page_size']) ? $_GET['page_size'] : '15';
        $status = isset($_GET['status']) ? $_GET['status'] : 0;
        $wsql = '';
        if ($status == '1') {
            $wsql .= ' f.tbzt = ' . $status . ' ';
        } elseif ($status == '2') {
            $wsql .= ' f.tbzt = 0 ';
        }
        if (!empty($wsql)) {
            $wsql = ' where ' . rtrim($wsql, ' and ');
        }
        $sql = "select f.*,u.nick,c.name from " . table("fbxq")." as f left join ".table('member')." as u on f.user_id = u.uid left join ".table('category')." as c on f.goods_fl = c.cid " . $wsql . " order by f.addtime desc limit " . ($page - 1) * $page_size . "," . $page_size;
        $result = $this->db->get_all($sql);

        if ($result) {
            foreach ($result as $key => $value){
                $goods = unserialize($value['goods_guige']);
                $guige = '';
                foreach ($goods as $g_value){
                    foreach ($g_value as $g_k => $g_v){
                        if($g_k == 0){
                            $guige .= '<a class="guige">'.$g_v.'：</a>';
                        }else{
                            $guige .= $g_v;
                        }
                    }
                }
                $result[$key]['goods_guige'] = $guige;
            }
            $data['list'] = $result;
            $total = $this->db->get_one("select count(*) as total from " . table("fbxq")." as f left join ".table('member')." as u on f.user_id = u.uid left join ".table('category')." as c on f.goods_fl = c.cid " . $wsql);
            if ($total > $page_size) {
                $data['page'] = $this->uqcms_page(array('page' => $page, 'page_size' => $page_size, 'total' => $total['total']));
            }
            $this->assign('data', $data);
        }
        $this->display();
    }
    function seller_detail()
    {
        $id = $_GET['id'];
        if(!empty($id)){
            $sql = "select f.*,u.nick,c.name from " . table('fbxq') . " as f left join ".table('member')." as u on f.user_id = u.uid left join ".table('category')." as c on f.goods_fl = c.cid where id = '{$id}' limit 1";
            $detail = $this->db->get_one($sql);
            $goods = unserialize($detail['goods_guige']);
            $guige = '';
            foreach ($goods as $kk => $g_value){
                foreach ($g_value as $g_k => $g_v){
                    if($g_k == 0 && $kk != 0){
                        $guige .= '<a class="guige">'.$g_v.'：</a>';
                    }else{
                        $guige .= $g_v;
                    }
                }
            }
            $detail['goods_guige'] = $guige;
            $tb_sql = "select t.*,u.nick from ".table('tb')." as t left join ".table('member')." as u on t.mid = u.uid where tb_id = ".$id;
            $tb = $this->db->get_all($tb_sql);
            $this->assign('tb',$tb);
            $this->assign('detail',$detail);
        }else{
            header("Location:admin.php?m=goods_order&a=seller");
        }
        $this->display();
    }
}